package com.boot.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;

@Component
public class SimpleRealms extends AuthorizingRealm {

    /**
     * 认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        System.out.println("SimpleRealms.doGetAuthenticationInfo.token:"+token.hashCode());
        String username = upToken.getUsername();

        String password = getPasswordForUser(username);

        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(username, password, getName());

        System.out.println("SimpleRealms.doGetAuthenticationInfo end");

        return info;
    }

    private String getPasswordForUser( String username){
        return "123456";
    }

    /**
     * 授权
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }


}

